﻿using System.ComponentModel;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media;
using MagicKey.Factory;
using MagicKey.Interfaces;
using MessageBox = System.Windows.MessageBox;

namespace MagicKey.UserInterface
{
    /// <summary>
    ///     Interaktionslogik für Registrierung.xaml
    /// </summary>
    public partial class Registration : Window
    {
        #region Members

        private static readonly IMagicKey MagicKey = MagicKeyFactory.GetInstance().GetMagicKeyInterface();
        private bool _registrationSuccessul = false;

        #endregion

        #region Properties

        private bool CanSave
        {
            get;
            set;
        }

        #endregion

        #region Constructors

        public Registration()
        {
            InitializeComponent();

        }

        public Registration(MainWindow myWindow)
        {
            InitializeComponent();
            Owner = myWindow;
        }

        #endregion

        #region Methods

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(" Registration windows successfully loaded!!!!!");
        }

        private void SaveButtonClick(object sender, RoutedEventArgs e)
        {
            _registrationSuccessul = false;
            var isPasswordValid = (PasswordConfTextBox.Password.Length > 5) && (PasswordConfTextBox.Password == PasswordTextBox.Password);
            UsernameErrorLabel.Visibility = Visibility.Hidden;
            PasswordConfErrorLabel.Visibility = Visibility.Hidden;
            PasswordErrorLabel.Visibility = Visibility.Hidden;
            RoleErrorLabel.Visibility = Visibility.Hidden;

            //Set color set
            UsernameErrorLabel.Foreground = Brushes.Red;
            PasswordConfErrorLabel.Foreground = Brushes.Red;
            PasswordErrorLabel.Foreground = Brushes.Red;
            RoleErrorLabel.Foreground = Brushes.Red;

            if (UsernameTexBox.Text.Length > 2)
            {
                UsernameTexBox.Text = UsernameTexBox.Text.ToLower();
                if (isPasswordValid)
                {
                    if (!RoleComboBox.Text.Equals(""))
                    {
                        IAccessManager accessManager = MagicKey.GetAccessManager();
                        IFileAccess dataAccess = MagicKey.GetFileAccess();
                        IUserManager userManager = MagicKey.GetUserManager();

                        string salt = accessManager.GenerateSalt();

                        // Generate a key pair, public and private keys
                        string[] keyPair = accessManager.GenerateAsymKeysPair();

                        // Encrypt private key with user password
                        keyPair[1] = accessManager.EncryptStringSym(keyPair[1], PasswordConfTextBox.Password);

                        // Hash password
                        string hashedPassword = accessManager.HashPassword(PasswordConfTextBox.Password);

                        // Destroy a password
                        PasswordConfTextBox.Password = null;

                        // Hash a hashed password with salt
                        hashedPassword = accessManager.HashHashedPasswordAndSalt(hashedPassword, salt);

                        // Add user data into database and into a runtime user list
                        bool isUserdded = userManager.AddUser(UsernameTexBox.Text, hashedPassword, salt,
                            RoleComboBox.Text,
                            keyPair[0], keyPair[1]);
                        if (isUserdded)
                        {
                            _registrationSuccessul = true;
                            MessageBox.Show("Your registration was successful!\nPlease loging now.", "Registration",
                                MessageBoxButton.OK, MessageBoxImage.Information);
                            ((MainWindow) this.Owner).Init();
                            this.Close();
                        }
                        else
                        {
                            MessageBox.Show("Your registration failed", "Error",
                               MessageBoxButton.OK, MessageBoxImage.Information);
                        }
                        
                    }
                    else
                    {
                        // Comboy muss be entered
                        RoleErrorLabel.Content = "Select your role";
                        RoleErrorLabel.Visibility = Visibility.Visible;
                    }
                }
                else
                {

                    //password was not equals 
                    if (PasswordTextBox.Password.Equals(""))
                    {
                        PasswordErrorLabel.Content = "Password is empty";
                        PasswordErrorLabel.Visibility = Visibility.Visible;

                    }
                    else if (PasswordTextBox.Password.Length <= 5)
                    {
                        PasswordErrorLabel.Content = "Min. 6 characters";
                        PasswordErrorLabel.Visibility = Visibility.Visible;
                    }

                    if (PasswordConfTextBox.Password.Equals(""))
                    {
                        PasswordConfErrorLabel.Content = "Password is empty";
                        PasswordConfErrorLabel.Visibility = Visibility.Visible;


                    }
                    else if (PasswordConfTextBox.Password.Length <= 5)
                    {
                        PasswordConfErrorLabel.Content = "Min. 6 characters";
                        PasswordConfErrorLabel.Visibility = Visibility.Visible;
                    }
                    if (PasswordTextBox.Password.Length <= 5)
                    {
                        PasswordErrorLabel.Content = "Min. 6 characters";
                        PasswordErrorLabel.Visibility = Visibility.Visible;
                    }
                    else if (!PasswordConfTextBox.Password.Equals(PasswordTextBox.Password))
                    {
                        PasswordConfErrorLabel.Content = "Password not match";
                        PasswordConfErrorLabel.Visibility = Visibility.Visible;
                    }

                    if (RoleComboBox.Text.Equals(""))
                    {
                        // Combobox muss be entered
                        RoleErrorLabel.Content = "Select your role";
                        RoleErrorLabel.Visibility = Visibility.Visible;
                    }
                }
            }
            else
            {
                UsernameErrorLabel.Content = "Min. 3 characters";
                //Username min 2 chars
                UsernameErrorLabel.Visibility = Visibility.Visible;


                //password was not equals 
                if (PasswordTextBox.Password.Equals(""))
                {
                    PasswordErrorLabel.Content = "Password is empty";
                    PasswordErrorLabel.Visibility = Visibility.Visible;

                }
                else if (PasswordTextBox.Password.Length <= 5)
                {
                    PasswordErrorLabel.Content = "Min. 6 characters";
                    PasswordErrorLabel.Visibility = Visibility.Visible;
                }

                if (PasswordConfTextBox.Password.Equals(""))
                {
                    PasswordConfErrorLabel.Content = "Password is empty";
                    PasswordConfErrorLabel.Visibility = Visibility.Visible;


                }
                else if (PasswordConfTextBox.Password.Length <= 5)
                {
                    PasswordConfErrorLabel.Content = "Min. 6 characters";
                    PasswordConfErrorLabel.Visibility = Visibility.Visible;
                }
                if (PasswordTextBox.Password.Length <= 5)
                {
                    PasswordErrorLabel.Content = "Min. 6 characters";
                    PasswordErrorLabel.Visibility = Visibility.Visible;
                }
                else if (!PasswordConfTextBox.Password.Equals(PasswordTextBox.Password))
                {
                    PasswordConfErrorLabel.Content = "Password not match";
                    PasswordConfErrorLabel.Visibility = Visibility.Visible;
                }

                if (RoleComboBox.Text.Equals(""))
                {
                    // Comboy muss be entered
                    RoleErrorLabel.Content = "Select your role";
                    RoleErrorLabel.Visibility = Visibility.Visible;
                }
            }
        }

        private void CancelButtonClick(object sender, RoutedEventArgs e)
        {
            Close();
        }

        #endregion

        #region EventchangedMethods

        private void Registration_OnClosing(object sender, CancelEventArgs e)
        {
            if (!_registrationSuccessul)
            {
                string msg = "Close this window?";
            MessageBoxResult result = MessageBox.Show(msg, "Registration",
                MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (result == MessageBoxResult.No)
            {
                // If user doesn't want to close, cancel closure
                e.Cancel = true;
            }
            else
            {
                ((MainWindow)this.Owner).Init();
                e.Cancel = false;
            }
        }
    }

        #endregion EventchangedMethods
    }
}